<template>
    <el-dialog v-if="dialogVisible" :close-on-click-modal='false' title="标签设置" :visible.sync="dialogVisible" @close="resetForm('addMaterialTagForm')" width="400px">
        <el-form label-position="top" size="medium" :model="addMaterialTagForm" :hide-required-asterisk='true' ref="addMaterialTagForm" :rules="addMaterialTagRules" class="dialog_form">
            <el-form-item label="" prop="tag">
                <div slot="label" style="width:360px" class="flexbox flexcenter flexbetween">
                    <span>标签</span>
                    <el-link :underline="false" type="primary" @click="innerVisible = true">新建标签</el-link>
                </div>
                <el-select v-model="addMaterialTagForm.tag" multiple collapse-tags placeholder="请选择标签" style="width:100%" @change="seletTag">
                    <el-option :label="item.name" :value="item.id" v-for="item in taglist" :key="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="已添加标签" prop="title" style="margin-bottom:0">
                <el-row :gutter="12">
                    <el-col :span="8" v-for="(item,index) in has_tag" :key="item.id">
                        <div class="mt_tag_item"><span class="tag_maxlength">{{item.name}}</span> <span class="delet_tag el-icon-close" @click="deletTag(index,item.id)"></span></div>
                    </el-col>
                </el-row>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="resetForm('addMaterialTagForm')" class="el-button-width">取 消</el-button>
            <el-button size="small" type="primary" @click="submitForm('addMaterialTagForm')" :disabled="loading" :loading="loading" class="el-button-width">保 存</el-button>
        </span>

        <el-dialog width="400px" title="标签设置" :visible.sync="innerVisible" append-to-body @close="resetTag('addTagForm')">
            <el-form label-position="top" size="medium" :model="addTagForm" :hide-required-asterisk='true' ref="addTagForm" :rules="addTagRules" class="dialog_form">
                <el-form-item label="新建标签" prop="name" style="margin-bottom:0">
                    <el-input type="text" placeholder="请输入标签名称" v-model="addTagForm.name" maxlength="6" show-word-limit>
                    </el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="resetTag('addTagForm')" class="el-button-width">上一步</el-button>
                <el-button size="small" type="primary" @click="submitTag('addTagForm')" :disabled="loading_s" :loading="loading_s" class="el-button-width">确 定</el-button>
            </span>
        </el-dialog>
    </el-dialog>
</template>

<script>
export default {
    props: ["taglist"],
    name: "",
    data() {
        return {
            loading: false,
            loading_s: false,
            dialogVisible: false,
            innerVisible: false,
            // tag_list: [],
            has_tag: [],
            type: null,
            addMaterialTagForm: {
                id: "",
                tag: [],
                source_type: null,
            },
            addTagForm: {
                name: "",
            },
            addTagRules: {
                name: [
                    {
                        required: true,
                        message: "请输入标签名称",
                        trigger: ["change"],
                    },
                ],
            },
        };
    },
    created() {
        
    },
    beforeDestroy() {
        
    },
    methods: {
        // 选择标签
        seletTag() {
            let arr = [];
            for (let i in this.taglist) {
                for (let a in this.addMaterialTagForm.tag) {
                    if (this.taglist[i].id == this.addMaterialTagForm.tag[a]) {
                        arr.push(this.taglist[i]);
                    }
                }
            }
            this.has_tag = arr;
        },

        // 删除标签
        deletTag(index, id) {
            for (let a in this.addMaterialTagForm.tag) {
                if (this.addMaterialTagForm.tag[a] == id) {
                    this.addMaterialTagForm.tag.splice(a, 1);
                }
            }
            this.has_tag.splice(index, 1);
        },

        //获取数据列表
        async getTagList(query) {
            const { data: res } = await this.$http.post(
                "adminapi/material/tagList",
                {
                    page: 1,
                    limit: 0,
                    query:query ? query : undefined
                }
            );
            if (res.code !== 200) {
                this.loading = false;
                this.$message.error(res.msg);
                return;
            } else {
                this.tag_list = res.data.list;
            }
        },

        // 提交添加
        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    this.loading = true;
                    console.log(this.addMaterialTagForm)
                    var { data: res } = await this.$http.post(
                        "adminapi/material/tagSign",
                        this.addMaterialTagForm
                    );
                    if (res.code !== 200) {
                        this.$message.error(res.msg);
                        this.loading = false;
                        return;
                    } else {
                        this.$message({
                            message: "保存成功",
                            type: "success",
                        });
                        this.loading = false;
                        this.resetForm(formName);
                        this.$parent.getDataList(1);
                    }
                } else {
                    this.loading = false;
                    return false;
                }
            });
        },

        // 关闭重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.dialogVisible = false;
        },

        // 提交添加
        submitTag(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    this.loading_s = true;
                    const { data: res } = await this.$http.post(
                        "adminapi/material/tagAdd",
                        this.addTagForm
                    );
                    if (res.code !== 200) {
                        this.$message.error(res.msg);
                        this.loading_s = false;
                        return;
                    } else {
                        this.$message({
                            message: "添加成功",
                            type: "success",
                        });
                        this.loading_s = false;
                        this.resetTag(formName);
                        this.getTagList(1);
                        this.$parent.getTagList(1);
                    }
                } else {
                    this.loading_s = false;
                    return false;
                }
            });
        },

        // 关闭重置表单
        resetTag(formName) {
            this.$refs[formName].resetFields();
            this.innerVisible = false;
        },
    },
    components: {},
};
</script>


<style scoped>
.mt_tag_item {
    height: 32px;
    line-height: 32px;
    background-color: rgba(165, 177, 209, 0.08);
    border-radius: 2px;
    padding: 0 8px;
    margin-bottom: 10px;
    position: relative;
}
.tag_maxlength {
    display: inline-block;
    max-width: 90px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    height: 32px;
    line-height: 32px;
}
.delet_tag {
    position: absolute;
    top: 50%;
    right: 8px;
    transform: translateY(-50%);
    cursor: pointer;
}
</style>
